package org.py.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.py.common.until.StringUtils;


import java.util.*;

/**
 * 
 * @author anyi
 * 主要处理json返回数据的封装
 *
 */
public class MessageUtils {


	/**
	 * 对象转json
	 **/
	public static String ObjectTojson(MessageObject messageObject) {
		if (messageObject != null) {
			if (StringUtils.isNotNull(messageObject.getCode()) && StringUtils.isNotNull(messageObject.getContent())) {
				JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
				String json = JSON.toJSONString(messageObject, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteMapNullValue);
				return json;
			} else {
				//可能是数据为空
				return "请注意数据格式";
			}
		}
		return null;
	}

	/**
	 * json转 对象
	 *
	 * @param <T>
	 **/
	public static <T> Object JsonToObject(String json, Class<T> clazz) {
		Object obj = JSON.parseObject(json, clazz);
		return obj;
	}

	/**
	 * json 转目标对象
	 **/
	public static MessageObject ObectToClass(String json, Class<?> classs) {
		MessageObject messageObject = new MessageObject();
		if (json == null || json.isEmpty()) {
			messageObject.setCode(MessageCode.M1002);
			messageObject.setContent("您输入的数据不完整，请完善您输入的数据！");
		} else if (!JsonValidate(json, "content")) {
			messageObject.setCode(MessageCode.M1002);
			messageObject.setContent("您输入的数据不能解析，请检查您数据格式！");
		} else {
			messageObject = (MessageObject) MessageUtils.JsonToObject(json, MessageObject.class);
			if (messageObject == null) {
				messageObject.setCode(MessageCode.M1002);
				messageObject.setContent("您输入的数据无法解析为对象");
			} else {
				try {
					if ((messageObject.getContent() instanceof JSONObject) && StringUtils.isNotNull(messageObject.getCode())) {
						JSONObject content = (JSONObject) messageObject.getContent();
						Object object_cont = MessageUtils.JsonToObject(content.toJSONString(), classs);
						if (object_cont != null) {
							messageObject.setCode(MessageCode.M1000);
							messageObject.setContent(object_cont);
						}
					} else {
						messageObject.setCode(MessageCode.M1002);
						messageObject.setContent("您输入的数据不是json对象");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		}
		return messageObject;
	}

	/**
	 * json 转List目标对象
	 */
	public static MessageObject ObectToListClass(String json, Class<?> classs) {
		MessageObject messageObject = new MessageObject();
		if (json == null || json.isEmpty()) {
			messageObject.setCode(MessageCode.M1002);
			messageObject.setContent("您输入的数据不完整，请完善您输入的数据！");
		} else if (!JsonValidate(json, "content")) {
			messageObject.setCode(MessageCode.M1002);
			messageObject.setContent("您输入的数据不能解析，请检查您数据格式！");
		} else {
			messageObject = (MessageObject) MessageUtils.JsonToObject(json, MessageObject.class);
			if (messageObject == null) {
				messageObject.setCode(MessageCode.M1002);
				messageObject.setContent("您输入的数据无法解析为对象");
			} else {
				try {
					if ((messageObject.getContent() instanceof JSONObject || messageObject.getContent() instanceof JSONArray) && StringUtils.isNotNull(messageObject.getCode())) {
						Object content = messageObject.getContent();
						ArrayList<Object> json_list = new ArrayList<>();
						if (content instanceof JSONObject) {
							JSONObject jsonObject = (JSONObject) content;
							Object object_cont = MessageUtils.JsonToObject(jsonObject.toJSONString(), classs);
							json_list.add(object_cont);
						}
						if (content instanceof JSONArray) {
							JSONArray jsonArray = (JSONArray) content;
							for (Object jsonObject : jsonArray) {
								Object platformModel = JSONObject.parseObject(jsonObject.toString(), classs);
								json_list.add(platformModel);
							}
						}
						if (json_list != null && json_list.size() > 0) {
							messageObject.setCode(MessageCode.M1000);
							messageObject.setContent(json_list);
						}
					} else {
						messageObject.setCode(MessageCode.M1002);
						messageObject.setContent("您输入的数据不是json对象");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		}
		return messageObject;
	}

	/**
	 * json转map
	 *
	 * @param json
	 * @param classs
	 * @return
	 */
	public static MessageObject ObjectToMapClass(String json, Class<?> classs) {
		MessageObject messageObject = new MessageObject();
		if (json == null || json.isEmpty()) {
			messageObject.setCode(MessageCode.M1002);
			messageObject.setContent("您输入的数据不完整，请完善您输入的数据！");
		} else if (!JsonValidate(json, "content")) {
			messageObject.setCode(MessageCode.M1002);
			messageObject.setContent("您输入的数据不能解析，请检查您数据格式！");
		} else {
			messageObject = (MessageObject) MessageUtils.JsonToObject(json, MessageObject.class);
			if (messageObject == null) {
				messageObject.setCode(MessageCode.M1002);
				messageObject.setContent("您输入的数据无法解析为对象");
			} else {
				try {
					Object content = messageObject.getContent();
					Map<String, Object> json_map = new HashMap<String, Object>();
					JSONObject c = (JSONObject) content;
					JSONObject json2 = JSONObject.parseObject(String.valueOf(c));
					for (Object k : json2.keySet()) {
						Object v = json2.get(k);
						if (v == null) {
							json_map.put(k.toString(), "");
						}
						if (k instanceof JSONArray) {
							List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
							Iterator<Object> it = ((JSONArray) v).iterator();
							while (it.hasNext()) {
								JSONObject jsonObject = (JSONObject) it.next();
								Object platformModel = JSONObject.parseObject(jsonObject.toString(), classs);
								list.add((Map<String, Object>) platformModel);
								json_map.put(k.toString(), list);
							}
						} else {
							json_map.put(k.toString(), v);
						}
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return messageObject;
	}
	/**
	 *
	 * 检查是否json格式字符串
	 * */
	public static boolean JsonValidate(String json,String key) {
		boolean retrurnbool = false;
		try {
			JSONObject obj = JSONObject.parseObject(json);
			retrurnbool = obj.containsKey(key);
		}catch (JSONException ex){
			retrurnbool = false;
		}
		return retrurnbool;
	}
	public static void main(String[] args) {
		/**String json = "{\"code\":\"1000\",\"content\":{\"userName\":\"121212\"},\"current\":0,\"pages\":0,\"pagesize\":0}";
		List<SysUser> list = new ArrayList<>();
		for (int i = 0; i <5 ; i++) {
			SysUser sysUser = new SysUser();
			sysUser.setUserName(String.valueOf(i));
			list.add(sysUser);
		}
		MessageObject messageObject = new MessageObject();
		messageObject.setCode("1000");
		messageObject.setContent(list);
		System.out.println("**************"+MessageUtils.ObjectTojson(messageObject));
		//MessageObject messageObject =  MessageUtils.ObectToClass(json, SysUser.class);
		MessageObject messageObject_json = MessageUtils.ObectToListClass(MessageUtils.ObjectTojson(messageObject), SysUser.class);
		System.out.println(""+messageObject_json.toString());**/
	}
}
